﻿<%@ Page Language="C#" MasterPageFile="~/NestedMasterPage.master" %>
<%@ Register TagPrefix="Page" TagName="SiteMap" Src="~/UserControls/SiteMapControl.ascx" %>
<%@ Register TagPrefix="Page" TagName="ProductDetail" Src="~/UserControls/ProductDetailControl.ascx" %>

<%@ Import Namespace="System.Web.Script.Serialization" %>

<script runat="server">
 
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      ProductDetailControl.ImgBanner = imgBanner;

      string B2C_CODE = Request.QueryString["B2C_CODE"];
      //取得商品關聯清單
      GetRelatedList(B2C_CODE);
      //紀錄商品點擊數
      AddProductClick(B2C_CODE);
    }
  }

  /// <summary>
  /// 紀錄商品點擊數
  /// </summary>
  /// <param name="b2c_code">商品的B2C_CODE</param>
  protected void AddProductClick(string b2c_code)
  {
    B2C_MALL_TRACKING ws = new B2C_MALL_TRACKING();
    ws.Url = ConfigurationManager.AppSettings["B2C_MALL_TRACKING"].ToString(); 
    ws.B2C_MALL_AddB2C_CTR(System.Configuration.ConfigurationManager.AppSettings["WebDeviceid"].ToString()
      , HttpContext.Current.User.Identity.Name, b2c_code);
  }
  
  /// <summary>
  /// 取得商品關聯清單
  /// </summary>
  /// <param name="b2c_code">商品的B2C_CODE</param>
  protected void GetRelatedList(string b2c_code)
  {
    B2C_MALL_HOME ws = new B2C_MALL_HOME();
    ws.Url = ConfigurationManager.AppSettings["B2C_MALL_HOME"].ToString(); 
    string jsonString = ws.B2C_MALL_GetProductDetail(System.Configuration.ConfigurationManager.AppSettings["WebDeviceid"].ToString()
      , HttpContext.Current.User.Identity.Name, b2c_code);
    JObject restoredObject = JsonConvert.DeserializeObject<JObject>(jsonString);
    if (restoredObject != null)
    {
      string Is_Valid = restoredObject["Is_Valid"].ToString();
      string msg = restoredObject["Msg"].ToString();

      if (Is_Valid == "Y")
      {
        //List<RelatedProduct> RelatedProducts = new List<RelatedProduct>();

        JArray jArray = (JArray)restoredObject["RELATED_PROD_Ary"];
        //foreach (var ja in jArray)
        //{
        //  if (!string.IsNullOrEmpty(ja["RELATED_PROD"].ToString()))
        //  {
        //    RelatedProduct rp = GetRelatedProduct(ja["RELATED_PROD"].ToString());
        //    if (rp!=null)
        //    {
        //      RelatedProducts.Add(rp);                          
        //    }            
        //  }
        //}
        ListViewRelated.DataSource = jArray.ToArray();
        ListViewRelated.DataBind();
        if (jArray.Count == 0)
        {
          divRelatedProduct.Visible = false;          
        }
      }
    }
  }

  /// <summary>
  /// 取得商品關聯明細
  /// </summary>
  /// <param name="b2c_code">商品的B2C_CODE</param>
  /// <returns></returns>
  //protected RelatedProduct GetRelatedProduct(string b2c_code)
  //{
  //  RelatedProduct product = null;    

  //  B2C_MALL_HOME ws = new B2C_MALL_HOME();
  //  string jsonString = ws.B2C_MALL_GetProductDetail(System.Configuration.ConfigurationManager.AppSettings["WebDeviceid"].ToString()
  //    , HttpContext.Current.User.Identity.Name, b2c_code);
  //  JObject restoredObject = JsonConvert.DeserializeObject<JObject>(jsonString);
  //  if (restoredObject != null)
  //  {
  //    string Is_Valid = restoredObject["Is_Valid"].ToString();
  //    string msg = restoredObject["Msg"].ToString();
  //    if (Is_Valid == "Y")
  //    {
  //      product = new RelatedProduct();
  //      product.B2C_CODE = b2c_code;
  //      dynamic img = restoredObject["IMAGE_PATH_M_Ary"];
  //      product.IMAGE_PATH_M = img[0].IMAGE_PATH_M;

  //      dynamic B2C_CODE_ary = restoredObject["B2C_CODE_ary"];
  //      product.B2C_CODE_NAME = B2C_CODE_ary[0].B2C_CODE_NAME;
  //    }
  //  }
  //  return product;
  //}
  
 
  //protected class RelatedProduct
  //{
  //  public string B2C_CODE { get; set; }
  //  public string B2C_CODE_NAME { get; set; }
  //  public string IMAGE_PATH_M { get; set; }
  //}
  
  
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="NestedContent" runat="server">

  <div class="simple-banner">
    <img id="imgBanner" src="~/Images/banners/SimpleBanner/Simple1.jpg" alt="Alternate Text" runat="server" />
  </div>


  <Page:ProductDetail runat="server" ID="ProductDetailControl" />

  <div class="plus-products green with-slide JS_listSlide" id="divRelatedProduct" runat="server">
    <h3>關聯商品</h3>
    <div class="products-item-container">
      <div class="slide-container">
        <div class="slide-area">
          <asp:ListView ID="ListViewRelated" runat="server">
            <ItemTemplate>
              <div class="products-item">
                <div class="image-container">
                  <%--<a href="javascript:void(window.open('../Products/ProductDetailPopup.aspx?B2C_CODE=<%# Eval("B2C_CODE") %>','','width=750px;Height=490px;menubar=no,location=no,resizable=no,toolbar=no;scrollbars=yes,status=no'));">
                    <img src='<%# Eval("IMAGE_PATH_M") %>'>
                  </a>--%>
                   <asp:HyperLink ID="HLProduct" runat="server" Target="_blank"
                    NavigateUrl='<%# "~/Products/ProductDetail.aspx?B2C_CODE=" + Server.HtmlEncode(Eval("RELATED_PROD").ToString()) %>'
                    ImageUrl='<%# Server.HtmlEncode(Eval("IMAGE_PATH_M").ToString()) %>' />
                </div>
                <div class="general-info">
                  <div class="top-table">
                    <p>
                      <%# Server.HtmlEncode(Eval("B2C_CODE_NAME").ToString()) %>
                    </p>
                  </div>
                </div>
              </div>
            </ItemTemplate>
          </asp:ListView>          
        </div>
      </div>
    </div>
  </div>
  <div class="go-to-top"><a href="#">回到頂端</a></div>
  

</asp:Content>

